#ifndef LogConfig_h__
#define LogConfig_h__


#include <string>
#include <list>
#include "tinyxml/tinyxml.h"

#pragma pack( push, 1 )
#define TAG_LOGCONFIG					"LogConfig"			
#define TAG_FILELOG					    "FileLog"			
#define ATTR_FILE_ENABLE	            "Enable"	
#define ATTR_FILE_MAXSIZE	            "MaxSize"	
#define ATTR_FILE_LOGLVL	            "LogLvl"		

#define TAG_MYSQLLOG					"MysqlLog"			
#define ATTR_MYSQL_ENABLE	            "Enable"	
#define ATTR_MYSQL_DBID	                "DBId"	
#define ATTR_MYSQL_TBName               "TBName"	
#define ATTR_MYSQL_LOGLVL	            "LogLvl"		
#define ATTR_MYSQL_MAXIDLE	            "MaxIdle"	
#define ATTR_MYSQL_MAXBATCH	            "MaxBatch"	




typedef struct __LOGFILE_CFG
{
	bool bEnable;
	unsigned int nMaxSize;
	unsigned int nLoglvl;
}LOGFILE_CFG;


typedef struct __LOG_MYSQL_CFG
{
	bool bEnable;
//	unsigned int nDBId;
	std::list<unsigned int> nDBId;
	std::string szTBName;
	unsigned int nLoglvl;
	unsigned int nMaxIdle;
	unsigned int nMaxBatchNum;
}LOG_MYSQL_CFG;

typedef struct __LOG_CONFIG
{
	LOGFILE_CFG _logFile;
	LOG_MYSQL_CFG _logMysql;
}LOG_CONFIG;

#pragma pack( pop )

class LogConfigParser
{
public:
	static	bool	XmlElement2LogCfg(TiXmlElement* pLogInfo, LOG_CONFIG& cfg)
	{
		if( NULL == pLogInfo )
			return false;

		const char*	tagName = pLogInfo->Value();

		if( ( tagName == NULL ) || _stricmp( tagName, TAG_LOGCONFIG ) )
			return false;

		int value(0);
		cfg._logFile.bEnable = false;
		const char *attr=NULL;
		TiXmlElement*	pFileLogCfg = pLogInfo->FirstChildElement(TAG_FILELOG);
		if (pFileLogCfg)
		{
			attr = pFileLogCfg->Attribute(ATTR_FILE_ENABLE);
			if (attr && _strcmpi(attr, "true") == 0)
				cfg._logFile.bEnable = true;

			pFileLogCfg->Attribute(ATTR_FILE_MAXSIZE, &value);
			cfg._logFile.nMaxSize = value;

			pFileLogCfg->Attribute(ATTR_FILE_LOGLVL, &value);
			cfg._logFile.nLoglvl = value;
		}

		cfg._logMysql.bEnable = false;
		TiXmlElement*	pMyLogCfg = pLogInfo->FirstChildElement(TAG_MYSQLLOG);
		if (pFileLogCfg)
		{
			attr = pMyLogCfg->Attribute(ATTR_MYSQL_ENABLE);
			if (attr && _strcmpi(attr, "true") == 0)
				cfg._logMysql.bEnable = true;
			
			attr = pMyLogCfg->Attribute(ATTR_MYSQL_DBID);
			if (attr)
			{
				cfg._logMysql.nDBId.clear();

				std::vector<std::string > dbLst = splitString(attr, ",");
				for (auto& it : dbLst)
				{
					cfg._logMysql.nDBId.emplace_back(atoi(it.c_str()));
				}
			}

			attr = pMyLogCfg->Attribute(ATTR_MYSQL_TBName);
			if (attr)
				cfg._logMysql.szTBName = attr;


			pMyLogCfg->Attribute(ATTR_MYSQL_LOGLVL, &value);
			cfg._logMysql.nLoglvl = value;

			pMyLogCfg->Attribute(ATTR_MYSQL_MAXIDLE, &value);
			cfg._logMysql.nMaxIdle = value;

			pMyLogCfg->Attribute(ATTR_MYSQL_MAXBATCH, &value);
			cfg._logMysql.nMaxBatchNum = value;
		}
		return true;
	}

	static	bool	Xml2DBLogCfg(const char* xmlString, LOG_CONFIG& cfg)
	{
		TiXmlDocument	doc;
		doc.Parse( xmlString );
		TiXmlElement*	pRoot = doc.RootElement();
		return XmlElement2LogCfg(pRoot, cfg);
	}
};

#endif // LogConfig_h__
